/*
 * Copyright (C) 2008 Matt Falkoski
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.falko.android.raven.util;

import java.lang.reflect.Field;

import javax.microedition.khronos.opengles.GL10;
import javax.microedition.khronos.opengles.GL11;
import javax.microedition.khronos.opengles.GL11Ext;

import android.content.Context;
import android.graphics.Bitmap;
import android.opengl.GLUtils;

import com.falko.android.raven.renderer.Texture;

public class AndroidUtil {

	public static int[] getAllResourceIDs(Class<?> c)
			throws IllegalArgumentException {

		Field[] IDFields = c.getFields();

		int[] IDs = new int[IDFields.length];

		try {
			for (int i = 0; i < IDFields.length; i++) {
				IDs[i] = IDFields[i].getInt(null);
			}
		} catch (Exception e) {
			throw new IllegalArgumentException();
		}
		return IDs;
	}

	public static int getResId(String variableName, Context context, Class<?> c) {

		try {
			Field idField = c.getDeclaredField(variableName);
			return idField.getInt(idField);
		} catch (Exception e) {
			e.printStackTrace();
			return -1;
		}
	}
	
	//This is pulled from TextureLibrary
	private static int mTextureNameWorkspace[] = new int[1];
	public static Texture loadTexture(GL10 gl, Bitmap bitmap, Texture texture) {

		assert gl != null;
		if (texture.loaded) {
			return texture;
		}

		gl.glGenTextures(1, mTextureNameWorkspace, 0);

		int error = gl.glGetError();
		assert error == GL10.GL_NO_ERROR;

		int textureName = mTextureNameWorkspace[0];

		gl.glBindTexture(GL10.GL_TEXTURE_2D, textureName);

		error = gl.glGetError();
		assert error == GL10.GL_NO_ERROR;

		gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER,
				GL10.GL_NEAREST);
		gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER,
				GL10.GL_LINEAR);

		gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S,
				GL10.GL_CLAMP_TO_EDGE);
		gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T,
				GL10.GL_CLAMP_TO_EDGE);

		gl.glTexEnvf(GL10.GL_TEXTURE_ENV, GL10.GL_TEXTURE_ENV_MODE,
				GL10.GL_MODULATE); // GL10.GL_REPLACE);

		GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);

		error = gl.glGetError();
		assert error == GL10.GL_NO_ERROR;

		int mCropWorkspace[] = new int[4];

		mCropWorkspace[0] = 0;
		mCropWorkspace[1] = bitmap.getHeight();
		mCropWorkspace[2] = bitmap.getWidth();
		mCropWorkspace[3] = -bitmap.getHeight();

		((GL11) gl).glTexParameteriv(GL10.GL_TEXTURE_2D,
				GL11Ext.GL_TEXTURE_CROP_RECT_OES, mCropWorkspace, 0);

		texture.name = textureName;
		texture.width = bitmap.getWidth();
		texture.height = bitmap.getHeight();

		error = gl.glGetError();
		assert error == GL10.GL_NO_ERROR;

		texture.loaded = true;
		
		return texture;
	}

}
